package event.handlers;

import java.util.List;
import java.util.ListIterator;
import serializare.SerializationToXml;
import bean.Person;
import core.controller.EventHandler;

public class DeletePersonEventHandler extends EventHandler {

    @Override
    public String perform() {
        String nextUrl = getNextURL("OK");
        List<Person> persons = SerializationToXml.deserializeXMLToObject();
        int[] selectedId = event.getIntArray("id");
        for (int i = 0; i < selectedId.length; i++) {
            try {
                for (ListIterator<Person> iter = persons.listIterator(); iter.hasNext();) {
                    Person pers = iter.next();
                    if (selectedId[i] == pers.getId()) {
                        iter.remove();
                    }
                }
            } catch (UnsupportedOperationException e) {
                nextUrl = getNextURL("ERROR");
            } catch (IllegalStateException e) {
                nextUrl = getNextURL("ERROR");
            }
        }
        SerializationToXml.serializeObjectToXml(persons);
        return nextUrl;
    }
}
